/*
 * GLComponent.java  0.4.0 / Oct 23, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.opengl;

import com.cute4j.swing.Component;

/**
 * OpenGL component.
 * <br>
 * <br>
 * <em>NOTE: The OpenGL API will be changed in the future version.
 *           This is just proof of the concept.</em>
 * 
 * @cute4j.native
 *    generate="header"
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public abstract class GLComponent extends Component implements GLConstants {

// Static Initialization. ------------------------------------------------------

  static {
    if (OpenGL.SUPPORTED) {
      _initialize();
    }
  }

// Package-Private Field. ------------------------------------------------------

  long _delegateGL;

// Protected Constructors. -----------------------------------------------------

  /**
   * Constructs new GLComponent object.
   */
  protected GLComponent() {
    super(false);
    if (!OpenGL.SUPPORTED) {
      throw new GLException("OpenGL is not supported");
    }
    _delegateGL = _createGL();
    if (_delegateGL == 0) {
      throw new OutOfMemoryError("GLComponent creation failed");
    }
  }

// Public Methods. -------------------------------------------------------------

  /**
   * Updates the component.
   */
  public void updateGL() {
    disposedCheck();
    _updateGL(_delegateGL);
  }

// Protected Abstract Methods. -------------------------------------------------

// CS:OFF

  protected abstract void initializeGL();

  protected abstract void resizeGL(int width, int height);

  protected abstract void paintGL();

// Protected OpenGL Methods. ---------------------------------------------------

  /**
   * @deprecated will be removed in the future versions.
   */
  protected void glVertex3f(float x, float y, float z) {
    OpenGL._glVertex3f(x, y, z);
  }

  /**
   * @deprecated will be removed in the future versions.
   */
  protected void glColor3f(float r, float g, float b) {
    OpenGL._glColor3f(r, g, b);
  }

  /**
   * @deprecated will be removed in the future versions.
   */
  protected void glClearColor(float r, float g, float b, float a) {
    OpenGL._glClearColor(r, g, b, a);
  }

  /**
   * @deprecated will be removed in the future versions.
   */
  protected void glShadeModel(int mode) {
    OpenGL._glShadeModel(mode);
  }

  /**
   * @deprecated will be removed in the future versions.
   */
  protected void glClearDepth(float depth) {
    OpenGL._glClearDepth(depth);
  }

  /**
   * @deprecated will be removed in the future versions.
   */
  protected void glHint(int target, int mode) {
    OpenGL._glHint(target, mode);
  }

  /**
   * @deprecated will be removed in the future versions.
   */
  protected void glEnable(int cap) {
    OpenGL._glEnable(cap);
  }

  /**
   * @deprecated will be removed in the future versions.
   */
  protected void glDepthFunc(int func) {
    OpenGL._glDepthFunc(func);
  }

  /**
   * @deprecated will be removed in the future versions.
   */
  protected void glClear(int mask) {
    OpenGL._glClear(mask);
  }

  /**
   * @deprecated will be removed in the future versions.
   */
  protected void glLoadIdentity() {
    OpenGL._glLoadIdentity();
  }

  /**
   * @deprecated will be removed in the future versions.
   */
  protected void glTranslatef(float x, float y, float z) {
    OpenGL._glTranslatef(x, y, z);
  }

  /**
   * @deprecated will be removed in the future versions.
   */
  protected void glRotatef(float angle, float x, float y, float z) {
    OpenGL._glRotatef(angle, x, y, z);
  }

  /**
   * @deprecated will be removed in the future versions.
   */
  protected void glBegin(int mode) {
    OpenGL._glBegin(mode);
  }

  /**
   * @deprecated will be removed in the future versions.
   */
  protected void glEnd() {
    OpenGL._glEnd();
  }

  /**
   * @deprecated will be removed in the future versions.
   */
  protected void glViewport(int x, int y, int width, int height) {
    OpenGL._glViewport(x, y, width, height);
  }

  /**
   * @deprecated will be removed in the future versions.
   */
  protected void glMatrixMode(int mode) {
    OpenGL._glMatrixMode(mode);
  }

  /**
   * @deprecated will be removed in the future versions.
   */
  protected void gluPerspective(double fovy, double aspect, double zNear,
                                double zFar) {
    OpenGL._gluPerspective(fovy, aspect, zNear, zFar);
  }

// CS:ON

// Package-Private Native Methods. ---------------------------------------------

  native long _createGL();

  native void _updateGL(long delegate);

// Private Static Native Methods. ----------------------------------------------

  private static native void _initialize();

}
